package com.weasel.weixin.commons.helper;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;

import java.util.Arrays;

/**
 * Created by Dylan on 2015/9/28.
 */
public final class SignatureHelper {


    private SignatureHelper(){}

    /**
     * 验证签名
     * @param token
     * @param timestamp
     * @param nonce
     * @param signature
     * @return
     */
    public static boolean check(String token,String timestamp,String nonce,String signature){

       return StringUtils.equals(create(token,timestamp,nonce),signature);
    }

    /**创建签名
     * @param token
     * @param timestamp
     * @param nonce
     * @return
     */
    public static String create(String token,String timestamp,String nonce){
        String [] _arrays = new String[]{token,timestamp,nonce};
        Arrays.sort(_arrays);
        StringBuilder builder = new StringBuilder();
        for(String str : _arrays){
            builder.append(str);
        }
        return DigestUtils.sha1Hex(builder.toString());
    }


}
